package com.itheima.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.itheima.reggie.common.R;
import com.itheima.reggie.mapper.ShoppingCartMapper;
import com.itheima.reggie.pojo.ShoppingCart;
import com.itheima.reggie.service.ShoppingCartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class ShoppingCartServiceImpl implements ShoppingCartService {
    @Autowired
    private ShoppingCartMapper shoppingCartMapper;

    @Override
    @Transactional
    public R add(ShoppingCart shoppingCart) {
        if (shoppingCart.getDishId() != null) {
            LambdaQueryWrapper<ShoppingCart> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(ShoppingCart::getUserId, shoppingCart.getUserId())
                    .eq(ShoppingCart::getDishId, shoppingCart.getDishId())
                    .eq(ShoppingCart::getDishFlavor, shoppingCart.getDishFlavor());
            List<ShoppingCart> shoppingCarts = shoppingCartMapper.selectList(wrapper);
            inOrUpData(shoppingCart, shoppingCarts);
        } else {
            LambdaQueryWrapper<ShoppingCart> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(ShoppingCart::getUserId, shoppingCart.getUserId())
                    .eq(ShoppingCart::getSetmealId, shoppingCart.getSetmealId());
            List<ShoppingCart> shoppingCarts = shoppingCartMapper.selectList(wrapper);
            inOrUpData(shoppingCart, shoppingCarts);
        }
        LambdaQueryWrapper<ShoppingCart> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ShoppingCart::getUserId, shoppingCart.getUserId());
        List<ShoppingCart> shoppingCarts = shoppingCartMapper.selectList(wrapper);
        return R.success(shoppingCarts);
    }

    @Override
    public R clean(Long userId) {
        LambdaUpdateWrapper<ShoppingCart> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(ShoppingCart::getUserId, userId);
        shoppingCartMapper.delete(wrapper);
        return R.success("清空成功");
    }

    @Override
    public R list(Long userId) {
        LambdaQueryWrapper<ShoppingCart> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ShoppingCart::getUserId, userId);
        List<ShoppingCart> shoppingCarts = shoppingCartMapper.selectList(wrapper);
        return R.success(shoppingCarts);
    }

    @Override
    public R sub(ShoppingCart shoppingCart, Long userId) {
        LambdaQueryWrapper<ShoppingCart> cartwrapper = new LambdaQueryWrapper<>();
        cartwrapper.eq(ShoppingCart::getDishId, shoppingCart.getDishId())
                .eq(ShoppingCart::getUserId, userId);
        ShoppingCart shoppingCart1 = shoppingCartMapper.selectOne(cartwrapper);
        if (shoppingCart1.getNumber() - 1 > 0) {
            shoppingCart1.setNumber(shoppingCart1.getNumber() - 1);
            shoppingCartMapper.updateById(shoppingCart1);
        } else {
            shoppingCartMapper.deleteById(shoppingCart1);
        }
        return R.success(shoppingCart1);
    }

    private void inOrUpData(ShoppingCart shoppingCart, List<ShoppingCart> shoppingCarts) {
        if (shoppingCarts.size() == 0) {
            shoppingCartMapper.insert(shoppingCart);
        } else {
            ShoppingCart shoppingCart1 = shoppingCarts.get(0);
            shoppingCart1.setNumber(shoppingCart1.getNumber() + 1);
            shoppingCartMapper.updateById(shoppingCart1);
        }
    }
}
